package com.codingfuture.erp_system.web.controller;

import com.codingfuture.erp_system.dto.PageData;
import com.codingfuture.erp_system.entity.Store;
import com.codingfuture.erp_system.service.StoreService;
import com.codingfuture.erp_system.util.CheckUtils;
import com.codingfuture.erp_system.util.Result;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/v2")
public class StoreController {

    @Autowired
    private StoreService storeService;
    private final String str = "";

    //通过ID查询（回显）
    @GetMapping("/store/selectone")
    public Result findById(Integer id, HttpSession httpSession) {
        log.debug("hello-hello");
        httpSession.setAttribute("id", id);
        Store store = storeService.findById(id);
        return store == null ? Result.error(500, "服务端错误") : Result.OK(store);
    }

    //添加
    @PostMapping("/store/add")
    public Result<?> insertStore(Store store) {
        String name = store.getName();
        Integer empuuid = store.getEmpuuid();
        if (name == null || Objects.equals(name, str)) {
            return Result.error(400, "请输入正确的名称");
        } else if (empuuid == null) {
            return Result.error(400, "请输入序号");
        }
        //校验仓库名称合法性
        else if (CheckUtils.checkChinese(store.getName())) {
            return Result.error(500, "仓库名称不是中文/过长");
        } else if (storeService.nameExist(name) == null) {
            boolean result = storeService.insertStore(name, empuuid);
            return result ? Result.ok(store) : Result.error(500, "添加失败");
        }
        return Result.error(400, "输入内容不能与已有内容重复");
    }

    //删除
    @DeleteMapping("/store")
    public Result deleteStore(Integer id) {
        if (id == null) {
            return Result.error(400, "id为空");
        } else {
            boolean r = storeService.deleteStore(id);
            return r ? Result.ok() : Result.error(500, "服务端错误");
        }
    }

    //修改
    @PostMapping("//store/update")
    public Result updateStore(Store store,
                              HttpSession httpSession) {
        Object id = httpSession.getAttribute("id");
        store.setUuid((Integer) id);
        String name = store.getName();
        Integer empuuid = store.getEmpuuid();
        if (name == null || Objects.equals(name, str)) {
            return Result.error(400, "请输入正确的名称");
        } else if (empuuid == null) {
            return Result.error(400, "请输入序号");
        }
        //校验仓库名称合法性
        else if (CheckUtils.checkChinese(store.getName())) {
            return Result.error(500, "仓库名称不是中文/过长");
        }
        //判断名称是否存在
        else if (storeService.nameExist(name) == null) {
            boolean result = storeService.updateStore(store);
            return result ? Result.ok() : Result.error(500, "错误");
        }
        return Result.error(400, "输入内容不能与已有内容重复");
    }

    //分页查询/模糊查询
    @GetMapping("/store/listByPage")
    public Result findByAll(@RequestParam(defaultValue = "1") int page,
                            @RequestParam(defaultValue = "10") int rows,
                            Store store) {
        Page<Store> page1 = PageHelper.startPage(page, rows);
        List<Store> storeList = storeService.findByAll(store);
        PageData<Store> data = new PageData<>(storeList, page1.getTotal());
        return Result.ok(data);
    }

//    //模糊查询
//    @GetMapping("/store/listByPage")
//    public Result findByName(@RequestParam(defaultValue = "1") int page,
//                             @RequestParam(defaultValue = "10") int rows,
////                             HttpSession httpSession,
//                             String name
//                        ) {
//        Page<Store> page1 = PageHelper.startPage(page, rows);
////        httpSession.setAttribute("name",name);
//        List<Store> storeList = storeService.findByName(name);
//        PageData<Store> data = new PageData<>(storeList, page1.getTotal());
//        return Result.ok(data);
//    }

}
